home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d3 / rettig.arc / TRSOURCE.EXE / INDEXKEY.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  10KB  |  283 lines

  1. /**********
  2. *  INDEXKEY.C
  3. *
  4. *  by Ralph Davis
  5. *  modified by Tom Rettig, Leonard Zerman
  6. *
  7. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  8. *
  9. *     SYNTAX:  INDEXKEY <expC1> [<expC2> ... <expCn> /p]
  10. *
  11. * PARAMETERS:  <expC> = filename to read index expression of
  12. *                       accepts wildcards
  13. *
  14. *              /p     = pause after one screenful of output
  15. *
  16. *    RETURNS:  Index key expression used to create <expC>
  17. *
  18. *    SPECIAL LINK NOTE:
  19. *             This program makes calls to _TR_FUNC.ASM, an assembly
  20. *             language routine that must be named in your link command.
  21. *********/
  22.  
  23. #include "trlib.h"
  24. #include "stdio.h"
  25.  
  26. static char new_dir[65];   /* Holds name of directory to change to
  27.                               (if change is necessary) */
  28.  
  29. TRTYPE main(argc, argv)
  30. int argc;
  31. char *argv[];
  32. {
  33.    int  get_drive(), curr_drive;  /* For current drive */
  34.    int  set_drive(), new_drive;   /* To change to new disk drive */
  35.    int  set_dir();                /* To change directories */
  36.    char *get_dir();               /* Returns current directory */
  37.    char *curr_dir;                /* Holds current directory */
  38.    static char filename[80];      /* Holds name of desired file */
  39.    char *curr_file;               /* Holds name of current matching file */
  40.    char *char_pos;                /* Holds position of backslash in
  41.                                      requested filespec */
  42.    int  status;
  43.    char *find_first();            /* DOS function 0x4E--find first
  44.                                      matching file */
  45.    char *find_next();             /* DOS function 0x4F--find next
  46.                                      matching file */
  47.    static char indexkey[101];     /* Holds index key expression */
  48.    int counter = 0;               /* Counts number of index keys
  49.                                      displayed--regulates screen pause */
  50.    int cl_args;                   /* Indicates whether command line
  51.                                      arguments were entered */
  52.    int scroll = FALSE;            /* Pause screen or not */
  53.    int i;
  54.  
  55.    if (argv[argc-1][0] == '/' && 
  56.             toupper(argv[argc-1][1]) == 'P')  /* /P (scroll) option specified */
  57.    { 
  58.       scroll = TRUE;
  59.       --argc;         /* Back up to next to last argument */
  60.    }
  61.       
  62.    if (argc == 1)     /* No filename entered */
  63.    {
  64.       printf("Please enter filename (return for *.NTX):  ");
  65.       gets( filename );
  66.       
  67.       /* If user presses <RETURN>, default to file name "*.NTX" */
  68.  
  69.       if (_tr_strlen(filename) == 0)
  70.          _tr_strcpy( filename, "*.NTX" );
  71.       ++argc;                 /* Bump argc artificially */
  72.       cl_args = FALSE;        /* No command line arguments */
  73.    }
  74.    else
  75.    {
  76.       cl_args = TRUE;         /* Command line arguments were entered */
  77.    }
  78.  
  79.    curr_drive = get_drive();  /* Get current drive */
  80.  
  81.    while (--argc)
  82.    {
  83.       i = 0;
  84.       if (cl_args)            /* If command line arguments entered,
  85.                                  copy current one into filename buffer */
  86.          _tr_strcpy( filename, argv[argc] );
  87.  
  88.  
  89.          if (filename[1] == ':')   /* Was drive specified? */
  90.          {
  91.             /* Convert drive letter to integer for DOS call */
  92.             new_drive = (toupper(filename[0]) - 'A');  
  93.     
  94.             status = set_drive(new_drive);  /* Change to indicated drive */
  95.  
  96.             if (status == (-1))
  97.             {
  98.                printf("\007Invalid drive specification:  %c\n\n",
  99.                                                  new_drive + 'A');
  100.                continue;
  101.             }
  102.          }
  103.          else
  104.          {
  105.             new_dir[i++] = (curr_drive + 'A');  /* Put drive letter and ':'
  106.                                                    in full path name of
  107.                                                    file */
  108.             new_dir[i++] = ':';
  109.          }
  110.  
  111.          /* Was path specified? */
  112.  
  113.          char_pos = _tr_strrchr(filename, '\\');  /* Find last occurrence of
  114.                                                      backslash in filename */
  115.          curr_dir = get_dir();                    /* Get current directory */
  116.  
  117.          if (char_pos != 0L)
  118.          {
  119.             /* Add pathname to new directory string by using
  120.                _tr_strncpy to copy everything up to the final
  121.                backslash */
  122.  
  123.             _tr_strncpy(&new_dir[i], filename, char_pos-filename);
  124.          
  125.             /* Now put the filename into the filename buffer */
  126.  
  127.             _tr_strcpy(filename, char_pos+1);
  128.             
  129.             status = set_dir(new_dir); /* Change to new directory */
  130.  
  131.             if (status == (-1))        /* Invalid path */
  132.             {
  133.                printf("\007Path not found:  %s\n\n",_tr_toup(new_dir));
  134.                set_dir(curr_dir);      /* Return to old directory */
  135.                set_drive(curr_drive);  /* Return to old drive */
  136.                continue;
  137.             }
  138.          }
  139.          else
  140.          {
  141.             _tr_strcpy(&new_dir[i], curr_dir); /* Place current directory
  142.                                                   into new_dir buffer */
  143.          }
  144.  
  145.          /* Get first matching file */
  146.  
  147.          curr_file = find_first(filename);
  148.  
  149.          if (curr_file == 0L)
  150.          {
  151.             printf("\007No matching files found:  %s\\%s\n\n",
  152.                       _tr_toup(new_dir),_tr_toup(filename));
  153.          }
  154.          else
  155.          {
  156.             while (curr_file != 0L)
  157.             {
  158.  
  159.                /* Put file's index key into indexkey buffer */
  160.  
  161.                status = get_ikey(curr_file, indexkey);
  162.                if (status != (-1))
  163.                {
  164.                      /* Print filespec and index key */
  165.  
  166.                      printf("%s\\%s:\n\t%s\n\n",_tr_toup(new_dir),curr_file,
  167.                                            _tr_toup(indexkey));
  168.                      ++counter;
  169.  
  170.                   if (counter >= 7)
  171.                   {
  172.                      if (scroll)  /* Stop screen scrolling if /P
  173.                                      option specified */
  174.                      {
  175.                         printf("\nPress any key to continue...");
  176.                         getch();  /* Press any key */
  177.                         printf("\n\n");   /* Two linefeeds */
  178.                      }
  179.                      counter = 0;   /* Reset line counter */
  180.                   }
  181.                }
  182.                curr_file = find_next();  /* Get next matching file */
  183.  
  184.             } /* ENDWHILE:  curr_file != 0L
  185.                             i.e., while matching files are found */
  186.          } /* ENDIF:  curr_file == 0L  */
  187.  
  188.          set_dir(curr_dir);  /* Return to old directory */
  189.          set_drive(curr_drive);  /* and old drive */
  190.  
  191.    }         /* ENDWHILE:  argc-- */
  192. }            /* End of main procedure */
  193.  
  194.  
  195.  
  196. int get_ikey(s,t)   /* Extract index key from file */
  197. char *s;
  198. char *t;
  199. {
  200.    int status, fd;
  201.    int i;
  202.    char thischar;    /* Buffer to read single character from file into */
  203.    long offset;      /* File seek offset */
  204.    long file_pos;    /* Current file position */
  205.    long filesize;    /* Size of file */
  206.  
  207.    fd = _tr_open(s, READONLY);     /* Open file for read only */
  208.  
  209.    i = 0;
  210.    if (fd == (-1))
  211.    {
  212.       printf("\007Error opening file %s\\%s\n\n",_tr_toup(new_dir),
  213.                                                  _tr_toup(s));
  214.  
  215.       t[i] = '\0';    /* Indexkey <== null string */
  216.       return(fd);
  217.  
  218.    }
  219.   
  220.    offset = 10L;      /* For dBASE II index file */
  221.  
  222.    while (1)
  223.    {
  224.       filesize = _tr_lseek(fd, 0L, 2);  /* Get file size
  225.                                            by seeking 0 bytes from EOF */
  226.  
  227.       file_pos = _tr_lseek(fd, offset, 0);  /* Go to offset bytes from
  228.                                                beginning of file */
  229.  
  230.       if ((file_pos < (0L)) || (filesize < 25L))
  231.       {
  232.          printf("\007Invalid index file format:  %s\\%s\n\n",
  233.                       _tr_toup(new_dir), _tr_toup(s));
  234.          status = (-1);
  235.          break;
  236.       }
  237.  
  238.       status = _tr_read(fd, &thischar, 1);   /* Read one character
  239.                                                 at current file offset */
  240.   
  241.       if (status == (-1))
  242.       {
  243.          printf("\007Error reading file:  %s\\%s\n\n",
  244.                      _tr_toup(new_dir), _tr_toup(s));
  245.          break;
  246.       }
  247.  
  248.       if (isalpha(thischar))    /* If not alpha character, not index key */
  249.       {
  250.          _tr_lseek(fd, -1L, 1);      /* Back up one byte 
  251.                                         from current position  */
  252.          
  253.          do
  254.          { 
  255.              status = _tr_read(fd, &t[i], 1);  /* Read in one byte at a time */
  256.              if (status == (-1))
  257.              {
  258.                 printf("\007Error reading file:  %s\\%s\n\n",
  259.                                _tr_toup(new_dir), _tr_toup(s));
  260.                 break;
  261.               }
  262.          } while (t[i++] >= ' ');  /* Read in characters till we get
  263.                                       to one less than ASCII 0x20 */
  264.          break;
  265.       }
  266.       else
  267.       {
  268.          if (offset == 10L)
  269.             offset = 22L;     /* For Clipper */
  270.  
  271.          else if (offset == 22L)
  272.             offset = 24L;     /* For dBASE III */
  273.  
  274.          else
  275.             break;
  276.       }
  277.    }
  278.    t[i] = '\0';      /* Null terminate index key */
  279.    _tr_close(fd);
  280.    return(status);
  281. }
  282.  
  283.